package com.lilike.music.ss.controller.base;

import com.lilike.music.common.MessageBean;
import com.lilike.music.ss.service.impl.OSSFileUploadServiceImpl;
import com.lilike.music.utils.DateCommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileUploadController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadController.class);

    @Autowired
    private OSSFileUploadServiceImpl ossFileUploadService;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public MessageBean upload(@Param("filepath") String filepath){
        System.out.println("filepath:" + filepath);
        /** 当前系统时间【日期格式：YYYYMMDD】 **/
        String NOW_DATE = DateCommonUtils.currentDay();

        if( StringUtils.isEmpty(filepath) ){
            LOGGER.warn("[{}-FileUploadController] filepath is null, 要上传文件路径为空.", NOW_DATE);
            return MessageBean.error("上传失败，请选择您要上传的文件.");
        }

        /**
         * 与前端预定好，若上传多个文件，则使用英文','分割
         */
        String[] paths = filepath.split(",");
        if( paths == null || paths.length == 0 ){
//            System.out.println(paths[0]);
            LOGGER.warn("[{}-FileUploadController] filepath is null, 要上传文件路径为空.", NOW_DATE);
            return MessageBean.error("上传失败，请选择您要上传的文件.");
        }

        MessageBean messageBean = MessageBean.error("未知错误，请稍后重试.");

        if( paths.length == 1){
            messageBean = ossFileUploadService.upload(paths[0]);
        }else{
            messageBean = ossFileUploadService.upload(paths);
        }
        return messageBean;
    }
}
